using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
namespace com.dtn.deals
{
	public class tblFilterManager : ItblFilterManager
	{
		private static ItblFilterManager itblFilterManager = null;
		private tblFilterManager()
		{
		}
		public static ItblFilterManager CreateInstance()
		{
			if (itblFilterManager == null)
			{
				itblFilterManager = new tblFilterManager();
			}
			return itblFilterManager;
		}
		public int InserttblFilter(tblFilter tblfilter)
		{
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@FilterId", SqlDbType.Int),
																new SqlParameter("@GroupFilterId", SqlDbType.Int),
																new SqlParameter("@FilterName", SqlDbType.NVarChar),
																new SqlParameter("@FilterDescription", SqlDbType.NVarChar),
															};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = tblfilter.GroupFilterId;
			parameters[2].Value = tblfilter.FilterName;
			parameters[3].Value = tblfilter.FilterDescription;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_InserttblFilter", CommandType.StoredProcedure, parameters);
				tblfilter.FilterId = Convert.ToInt32(parameters[0].Value);
			}
			catch (Exception ex)
			{
				tblfilter.FilterId = -1;
			}
			return tblfilter.FilterId;
		}
		public bool UpdatetblFilter(tblFilter tblfilter)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@FilterId", SqlDbType.Int),
																new SqlParameter("@GroupFilterId", SqlDbType.Int),
																new SqlParameter("@FilterName", SqlDbType.NVarChar),
																new SqlParameter("@FilterDescription", SqlDbType.NVarChar),
															};
			parameters[0].Value = tblfilter.FilterId;
			parameters[1].Value = tblfilter.GroupFilterId;
			parameters[2].Value = tblfilter.FilterName;
			parameters[3].Value = tblfilter.FilterDescription;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_UpdatetblFilter", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public bool DeletetblFilter(int id)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@FilterId", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_DeletetblFilter", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public tblFilter SelecttblFilterByID(int id)
		{
			IDataReader dr;
			tblFilter tblfilter = new tblFilter();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@FilterId", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				dr = dataProvider.ExecuteReader("sp_SelecttblFilter", CommandType.StoredProcedure, parameters);
				while(dr.Read())
				{
					tblfilter.FilterId = Convert.ToInt32(dr["FilterId"].ToString());
					tblfilter.GroupFilterId = Convert.ToInt32(dr["GroupFilterId"].ToString());
					tblfilter.FilterName = (dr["FilterName"].ToString());
					tblfilter.FilterDescription = (dr["FilterDescription"].ToString());
				}

			}
			catch (Exception ex)
			{
				return null;
			}
			return tblfilter;
		}
		public DataTable SelecttblFiltersDynamic(string Where, string Order)
		{
			DataSet ds = new DataSet();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
			parameters[0].Value = Where;
			parameters[1].Value = Order;
			try
			{
				ds = dataProvider.ExecuteDataSet("sp_SelecttblFiltersDynamic", CommandType.StoredProcedure, parameters);
			}
			catch (Exception ex)
			{
				return null;
			}
			return ds.Tables["Table"];
		}
	}
}
